#!/usr/bin/python
# -*- coding: utf-8 -*-
#author: Xiao Yujie
#update: 2021-03-26
#version: 1.0.0

import sys, os
from .class_object_generator import object_generator as og
from communication_client.communication_client import Client
import json
from . import config

if sys.version_info.major == 3:
    raw_input = input

def main():
    def creat_kwargs():
        key, value = i.split('=')
        if 'pg' in key:
            print(i)
            pg_kwargs[key.lower()] = value
        elif 'skip_confirm' in key:
            skip_confirm = value
            return skip_confirm
        else:
            kwargs[key.lower()] = value

    params_list = sys.argv[1:]

    skip_confirm = False
    arg = ''
    args = []
    kwargs = {}
    pg_kwargs = {}

    if not params_list:
        obj_admin = og(pg_host=config.pg_host, pg_port=config.pg_port, pg_user=config.pg_user, pg_passwd=config.pg_passwd, pg_db=config.pg_db)
        obj_admin.object_generate()
    
    elif os.path.isfile(params_list[0]) or os.path.isdir(params_list[0]):
        if len(params_list) == 1:
            obj_admin = og()
            obj_admin.object_generate(params_list[0])

        else:
            print(params_list)
            for i in params_list[1:]:
                if '=' in i:
                    if creat_kwargs():
                        skip_confirm = True
                else:
                    args.append(i)

            obj_admin = og(*args, skip_confirm=skip_confirm, **kwargs)

            if 'observer' in kwargs:
                del kwargs['observer']
            if pg_kwargs:
                obj_admin.set_pg(**pg_kwargs)
            obj_admin.object_generate(params_list[0], **kwargs)

    else:
        for i in params_list:
            if '=' in i:
                if creat_kwargs():
                    skip_confirm = True
            else:
                arg += i + '\t'

        obj_admin = og(skip_confirm=skip_confirm, **kwargs)
        if 'observer' in kwargs:
            del kwargs['observer']
        if pg_kwargs:
            obj_admin.set_pg(**pg_kwargs)

        obj_admin.object_generate(arg, **kwargs)

    if obj_admin.object_params:
        # print(skip_confirm)
        print(u'目标参数：\n')
        for i in obj_admin.object_params:
            print(i)
        print('len: ', len(str(obj_admin.object_params)))
        pg_action = pg_kwargs['pg_action'] if 'pg_action' in pg_kwargs else 'insert'

        if pg_action == 'insert':
            print(u'\n确认写入数据库： Y/n')
        elif pg_action == 'delete':
            print(u'\n确认从数据库删除： Y/n')

        if raw_input('>>> ').lower() == 'y':
            client = Client(host=config.host, port=config.port, client_type=config.client_type)
            obj_admin.pg_proc_object_params(pg_action)
            
            client.Send()
            raw_input('ok')

def obj_add(*args, **kwargs):
    if args:
        if sys.version_info.major == 2:
            args = map(lambda x: x.encode('utf-8'), args)
        args_list = args[0].replace(',', ' ').split()
        for i in args_list:
            key, value = i.split('=')
            kwargs[key] = value
        # print(kwargs)

    for i in dir(config):
        if 'pg_' in i and not kwargs.get(i):
            kwargs[i] = getattr(config, i)

    obj_admin = og(skip_confirm=True, **kwargs)

    obj_admin.object_generate(*args, **kwargs)

    if 'pg_action' in kwargs:
        obj_admin.pg_proc_object_params(kwargs['pg_action'])
    else:
        obj_admin.pg_proc_object_params('insert')

    # client = Client('object_generator')
    # client.Send()

if __name__ == "__main__":
    main()
